#include "data_package.h"

uint8_t command_recv_buffer[512];

data_package::data_package(QObject *parent)
    : QObject{parent}
{

}

void data_package::receive_data(QByteArray array)
{
    uint16_t analysis_steps;
    uint16_t data_bytes;
    uint16_t data_recv_index;
    uint16_t verf_add;
    uint16_t verf_recv;
    // const uint8_t *recv_buff = (uint8_t *)array.data();
    const uint16_t recv_length = array.size();
    uint16_t get_index=0;
    analysis_steps = 0;
    while(get_index < recv_length)
    {
        //uint8_t dat = recv_buff[get_index++];
        uint8_t dat = array.at(get_index++);
        switch(analysis_steps)
        {
        case 0:
        {
            if(dat == 0x55)
            {
                analysis_steps = 1;
            }
        }
        break;
        case 1:
        {
            if(dat == 0xAA)
            {
                analysis_steps = 2;
                verf_add = 0;
            }
            else
            {
                analysis_steps = 0;
            }
        }
        break;
        case 2:
        {
            data_bytes = dat;
            analysis_steps = 3;
            verf_add += dat;
        }
        break;
        case 3:
        {
            data_bytes |= (uint16_t)dat << 8;
            if(data_bytes >= 2)
            {
                data_recv_index = 0;
                analysis_steps = 4;
                verf_add += dat;
            }
            else
            {
                analysis_steps = 0;
            }
        }
        break;
        case 4:
        {
            if(data_recv_index < (data_bytes-2))
            {
                verf_add += dat;
            }
            command_recv_buffer[data_recv_index] = dat;
            //command_recv_buffer.append((char)dat);
            if(++data_recv_index >= data_bytes)
            {
                analysis_steps = 0;
                verf_recv  = command_recv_buffer[data_bytes-2];
                verf_recv |= (uint16_t)command_recv_buffer[data_bytes-1] << 8;
                if(verf_recv == verf_add)
                {
                    cmd_data.clear();
                    cmd_data.append((char *)command_recv_buffer,data_bytes-2);
                    emit sig_command_receive(cmd_data);
                }
            }
        }
        break;
        default:
        {
            analysis_steps = 0;
        }
        break;
        }
    }
}

void data_package::create_data_package(QByteArray cmd_data,QByteArray &out_package)
{
    create_data_package((uint8_t *)cmd_data.data(),cmd_data.size(),out_package);
}

void data_package::create_data_package(uint8_t *cmd_data,size_t bytes,QByteArray &out_package)
{
    // uint16_t bytes = array.size();
    uint16_t len = bytes + 2;
    uint16_t verf_add ;
    // uint8_t *data_buff = (uint8_t *)array.data();
    uint8_t command_package_buffer[512];

    command_package_buffer[0] = 0x55;
    command_package_buffer[1] = 0xAA;
    command_package_buffer[2] = (uint8_t)( len      & 0x00FF);
    command_package_buffer[3] = (uint8_t)((len >> 8) & 0x00FF);
    verf_add  = command_package_buffer[2] ;
    verf_add += command_package_buffer[3] ;
    for(int i=0; i<bytes; i++)
    {
        verf_add += cmd_data[i];
        command_package_buffer[4+i] = cmd_data[i];
    }
    command_package_buffer[4+bytes+0] = (uint8_t)( verf_add       & 0x00FF);
    command_package_buffer[4+bytes+1] = (uint8_t)((verf_add >> 8) & 0x00FF);
    out_package.clear();
    out_package.append((const char *)command_package_buffer,4+bytes+2);
    //emit sig_send_data(data);
}
